package com.quatrix.openphantasy.client.ui;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.PropertySheet;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.Toolbar;
import com.smartgwt.client.widgets.tree.TreeGrid;

public class CRUDView extends VLayout {
	///////////////////////////////////////////////////////////////////////////
	// CRUD Toolbar
	///////////////////////////////////////////////////////////////////////////
	static class CRUDToolbar extends Toolbar {
		private final Button _add = new Button("Add");
		private final Button _edit = new Button("Edit");
		private final Button _delete = new Button("Delete");
		private final Button _refresh = new Button("Refresh");
		private final CRUDView _parent;
		CRUDToolbar(CRUDView view) {
			_parent = view;
			setWidth100();
			setHeight(24);
			
			// Setup buttons
			_add.setIcon("icons/add.png");
			_add.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					_parent._formPanel._form.editNewRecord();
					
				}				
			});
			
			_edit.setIcon("icons/edit.png");
			
			_delete.setIcon("icons/delete.png");
			_delete.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					_parent._grid.removeSelectedData();
				}
			});
			
			_refresh.setIcon("icons/refresh.png");
			_refresh.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					_parent._grid.fetchData();
				}
			});

			final HLayout pane = new HLayout(1);
			pane.addMember(_add);
			pane.addMember(_edit);
			pane.addMember(_delete);
			pane.addMember(_refresh);
			
			addChild(pane);
		}
	}
	///////////////////////////////////////////////////////////////////////////
	// Form Panel
	///////////////////////////////////////////////////////////////////////////
	static class FormPanel extends VLayout {
		private final Button _save = new Button("Save");
		
//		private final PropertySheet _form = new PropertySheet();
		private DynamicForm _form = TeamForm.createWidget();
		FormPanel() {
			super(10);
			// Setup "Save" button
			_save.setIcon("icons/save.png");
			_save.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					_form.saveData();
				}
				
			});
			
			_form.setIsGroup(true);
			_form.setGroupTitle("Properties");
			setWidth100();
			addMember(_form);
			addMember(_save);
		}
		
		public DynamicForm getForm() {
			return _form;
		}
	}
	
	DataSource _ds;
	
	final Toolbar _toolbar = new CRUDToolbar(this);
	
	final ListGrid _grid = new ListGrid();
	
	final FormPanel _formPanel  = new FormPanel();
	
	CRUDView() {
		// Setup grid
		_grid.setAutoFetchData(true);
		_grid.setAlternateRecordStyles(true);
		_grid.setWidth100();
		_grid.addRecordClickHandler(new RecordClickHandler() {

			public void onRecordClick(RecordClickEvent event) {
				ListGridRecord record = event.getRecord();
				_formPanel.getForm().editRecord(record);
			}
			
		});
		
		
		addMember(_toolbar);
		
		final HLayout panel = new HLayout(5);
		panel.addMember(_grid);
		panel.addMember(_formPanel);
		
		addMember(panel);
	}
	
	public DataSource getDataSource() {
		return _ds;
	}
	
	public void setDataSource(DataSource ds) {
		_ds = ds;
		_grid.setDataSource(_ds);
	}
	
	public static CRUDView createWidget(DataSource ds) {
		CRUDView retval = new CRUDView();
		retval.setDataSource(ds);
		return retval;
	}
}
